package com.heima.media.controller;

import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.dto.CheckDto;
import com.heima.media.dto.NewsDto;
import com.heima.media.dto.NewsPageDto;
import com.heima.media.service.IWmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 自媒体图文内容信息表 前端控制器
 * </p>
 *
 * @author SpaceTraveller
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/api/v1/news")
@Api(tags = "自媒体图文内容信息表接口")
public class WmNewsController {

    @Autowired
    private IWmNewsService wmNewsService;

    /**
     * 查看自己发布的文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查看自己发布的文章", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "dto", value = "请求体对象", required = true, dataType = "NewsPageDto")
    public ResponseResult getNewsList(@RequestBody NewsPageDto dto) {
        return wmNewsService.getNewsList(dto);
    }

    /**
     * 通过ID获取文章信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID获取文章信息", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "id", value = "文章id", required = true, dataType = "Long")
    public ResponseResult getNewsById(@PathVariable Long id) {
        return wmNewsService.getNewsById(id);
    }

    /**
     * 通过ID获取文章信息
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "自媒体用户发布或者修改文章", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "dto", value = "前端传的", required = true, dataType = "NewsDto")
    public ResponseResult saveAndUpdateNews(@RequestBody NewsDto dto) throws Exception {
        return wmNewsService.saveAndUpdateNews(dto);
    }

    /**
     * 文章上架或者下架
     * @return
     */
    @PostMapping("/down_or_up")
    @ApiOperation(value = "文章上架或者下架", notes = "author:SpaceTraveller")
    @ApiImplicitParams(value =
            {@ApiImplicitParam(name="enable", value = "前端传的", required = true, dataType = "Integer"),
            @ApiImplicitParam(name="id", value = "前端传的", required = true, dataType = "Integer")})
    public ResponseResult upOrDownNews(@RequestBody Map<String,Integer> args) {
        Integer id = args.get("id");
        Integer enable = args.get("enable");
        return wmNewsService.upOrDownNews(id, enable);
    }

    /**
     * 管理员查看文章库
     * @param dto
     * @return
     */
    @PostMapping("/findPageByName")
    @ApiOperation(value = "管理员查看文章库", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "dto", value = "前端传的", required = true, dataType = "CheckDto")
    public PageResponseResult findPageByName(@RequestBody CheckDto dto){
        return wmNewsService.findPageByName(dto);
    }

    /**
     * 管理员查看指定文章
     * @param id
     * @return
     */
    @GetMapping ("/findNewsVoById/{id}")
    @ApiOperation(value = "管理员查看指定文章", notes = "author:SpaceTraveller")
    @ApiImplicitParam(name = "id", value = "前端传的", required = true, dataType = "Long")
    public ResponseResult getOneNewsById(@PathVariable Long id){
        return wmNewsService.getOneNewsById(id);
    }

}
